package com.ai.service.rocketMq;

import com.ai.model.param.BuildCloudPictureToAi;
import com.ai.util.FileDownloadUtil;
import com.ai.util.HttpRequestUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.SelectorType;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 *  因为对方传递的是算法的开启信号，所以需要做到顺序读，
 *   就需要将消费者的消费模式设置为同步消费顺序读
 * @Author jiaok
 * @Date 2023-11-08 15:18
 */
@Slf4j
@Service
@RocketMQMessageListener(topic = "feais-comment-imgmessage-topic",selectorExpression ="feais-comment-tag-imgmessage",
        consumeMode = ConsumeMode.ORDERLY,consumerGroup="feais-comment-imgmessage-topic",selectorType = SelectorType.TAG)
public class RocketMQConsumerService implements RocketMQListener<MessageExt> {

    @Value("${nginx.path.images}")
    private  String nginxImagesPath;

    @Value("${api.ai.cloudPicUrl}")
    private String aiQuestUrl;

    @Override
    public void onMessage(MessageExt messageExt) {
        String tags = messageExt.getTags();
        String messageBody = new String(messageExt.getBody());

        JSONObject outerJsonObject = JSONObject.parseObject(messageBody);
        JSONObject innerJsonObject = JSONObject.parseObject(outerJsonObject.getString("body"));


        log.info("=========================== #RocketMQConsumerService.onMessage#  ===========================");
        log.info("===========================  监听到有消息发送到mq  ===========================");
        log.info("===========================  tags:{}, body:{}  ===========================",tags,messageBody);
        log.info("===========================  JSON:{}  ===========================",innerJsonObject.toJSONString());


        pictureInfoSendToAi(aiQuestUrl,innerJsonObject);

    }

    private void pictureInfoSendToAi(String file,JSONObject jsonObject){
        log.info("------------------------- 图片信息转发至算法：{} --------------------------",file);
        log.info("------------------------- 请求url：{} --------------------------",aiQuestUrl);
        BuildCloudPictureToAi buildCloudPictureToAi = new BuildCloudPictureToAi();
        String createTime = jsonObject.getString("createTime");
        String remark = jsonObject.getString("remark");
        String serialnumber = jsonObject.getString("serialnumber");
        String fileUrl = jsonObject.getString("fileUrl");
        buildCloudPictureToAi.setFile(fileUrl);
        buildCloudPictureToAi.setRemark(remark);
        buildCloudPictureToAi.setCreateTime(createTime);
        buildCloudPictureToAi.setSerialnumber(serialnumber);
        String param = JSONObject.toJSONString(buildCloudPictureToAi);
        log.info("------------------------- 请求参数：{} --------------------------",param);
        String s = HttpRequestUtil.sendPost(aiQuestUrl, param);
        if (org.springframework.util.StringUtils.isEmpty(s) || "null".equals(s)) {
            log.info("------------------------- 第一次 网络请求失败！ --------------------------");
            String second = HttpRequestUtil.sendPost(aiQuestUrl, param);
                 if (org.springframework.util.StringUtils.isEmpty(second) || "null".equals(second)) {
                     log.info("------------------------- 第二次 网络请求失败！ --------------------------");
                     String third = HttpRequestUtil.sendPost(aiQuestUrl, param);
                         if (org.springframework.util.StringUtils.isEmpty(third) || "null".equals(third)) {
                             log.info("------------------------- 第三次 网络请求失败！ --------------------------");
                         }
                 }
            return;
        }
    }

}
